const AWS = require('aws-sdk');
const regions = require("../../common/awsRegions");
AWS.config.update({region: regions.region_ap_southeast_1});

const ec2 = new AWS.EC2();

const d = {};

d.associateAddress = (params) => new Promise((resolve, reject) => {
  ec2.associateAddress(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
      /*
      var params = {
        AllocationId: "eipalloc-64d5890a",
        InstanceId: "i-0b263919b6498b123"
      };
      data = {
        AssociationId: "eipassoc-2bebb745"
       }
       */
    }// successful response
  });
});

d.allocateAddress = (params) => new Promise((resolve, reject) => {
  ec2.allocateAddress(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
      /*
      var params = {
      };
       */
    }// successful response
  });
});

d.releaseAddress = (params) => new Promise((resolve, reject) => {
  ec2.releaseAddress(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
      /*
      var params = {
        AllocationId: "eipalloc-64d5890a"
      };
       */
    }// successful response
  });
});

d.describeAddresses = (params) => new Promise((resolve, reject) => {
  ec2.describeAddresses(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
      /*
      var params = {
      };
      data = {
        Addresses: [
           {
          Domain: "standard",
          InstanceId: "i-1234567890abcdef0",
          PublicIp: "198.51.100.0"
         },
           {
          AllocationId: "eipalloc-12345678",
          AssociationId: "eipassoc-12345678",
          Domain: "vpc",
          InstanceId: "i-1234567890abcdef0",
          NetworkInterfaceId: "eni-12345678",
          NetworkInterfaceOwnerId: "123456789012",
          PrivateIpAddress: "10.0.1.241",
          PublicIp: "203.0.113.0"
         }
        ]
       }
      */
    }// successful response
  });
});

d.createKeyPair = (params) => new Promise((resolve, reject) => {
  ec2.createKeyPair(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.describeKeyPairs = (params) => new Promise((resolve, reject) => {
  ec2.describeKeyPairs(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

d.describeInstances = (params) => new Promise((resolve, reject) => {
  ec2.describeInstances(params, function(err, data) {
    if (err) {
      reject(err);
    } // an error occurred
    else {
      resolve(data);
    }// successful response
  });
});

module.exports = d;